# -*- coding: utf-8 -*-
import redis
import pymongo
import scrapy
import json


from juqing.items import Actors, Juqings, Plays


class JuqingbaClassifySpider(scrapy.Spider):
    """
    拉取首页分类
        :param scrapy.Spider: Spider
    """
    name = 'juqingba_classify'
    allowed_domains = ['www.juqingba.cn']
    start_urls = ['http://www.juqingba.cn/']
    redis_pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
    myredis = redis.Redis(connection_pool=redis_pool)
    client = pymongo.MongoClient(host='localhost', port=27017)
    
    def parse(self, response):
        """
        docstring here
            :param self: self
            :param response: response object
        """
        print('classify')
        # classify = []
        # for sel in response.xpath('//div[@class="Nav1"]/ul/li'):
        #     item = dict()
        #     item['title'] = sel.xpath('a/text()').extract_first()
        #     item['link'] = sel.xpath('a/@href').extract_first()
        #     classify.append(item)
        # self.myredis.set('CACHE_JUQINGBA_CLASSIFY', json.dumps(classify))


    def get_classify(self, response):
        """
        获取分类列表
            :param self: self
            :param response: response object
        """
        tasks = []
        for sel in response.xpath('//ul[@class="m_Box9 FL"]/li'):
            link = sel.xpath('a/@href').extract_first()
            classify = link.split('/')
            print(classify)
            # 调用获取基本信息 获取任务列表
            # self.myredis.hsetnx('CACHE_JUQINGBA_CLASSIFY')

        next_page = response.xpath(
            '//ul[@class="pagelist FR"]/li[last()-3]/a/@href').extract_first()
        url = response.urljoin(next_page)
        # yield scrapy.Request(url=url, callback=self.get_classify)
